package com.ruoyi.central.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.ruoyi.cache.common.CacheConstants;
import com.ruoyi.central.domain.TopCentralOperationStationMaterial;
import com.ruoyi.central.mapper.TopCentralOperationStationMaterialMapper;
import com.ruoyi.central.publisher.PublisherUtils;
import com.ruoyi.central.publisher.event.OperationStationMaterialEvent;
import com.ruoyi.central.service.ITopCentralOperationStationMaterialService;
import com.ruoyi.common.core.service.ServiceImpl;
import com.ruoyi.common.enums.SignEnum;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 操作站点-物料Service业务层处理
 *
 * @author SunLuQuan
 * @date 2022-04-25
 */
@Service
@Slf4j
@CacheConfig(cacheNames = CacheConstants.EhcacheName.CONTINUAL_CACHE_NAME)
public class TopCentralOperationStationMaterialServiceImpl extends ServiceImpl<TopCentralOperationStationMaterialMapper,TopCentralOperationStationMaterial> implements ITopCentralOperationStationMaterialService
{
    /**
     * 查询操作站点-物料
     * 
     * @param id 操作站点-物料主键
     * @param columns 查询指定的列
     * @return 操作站点-物料
     */
    @Override
    public TopCentralOperationStationMaterial selectById(Integer id, SFunction<TopCentralOperationStationMaterial,?> ... columns){

        if(StringUtils.isNotEmpty(columns)){
            return selectOne(lambdaQuery().select(columns).eq(TopCentralOperationStationMaterial::getId,id));
        }
        return super.selectById(id);
    }

    /**
     * 查询操作站点-物料列表
     *
     * @param topCentralOperationStationMaterial 操作站点-物料
     * @return 操作站点-物料
     */
    @Override
    public List<TopCentralOperationStationMaterial> selectList(TopCentralOperationStationMaterial topCentralOperationStationMaterial)
    {
        return getBaseMapper().selectTopCentralOperationStationMaterialList(topCentralOperationStationMaterial);
    }

    /**
     * 查询操作站点-物料 根据操作站位编号
     * @param operationStationId 操作站位编号
     * @return 返回分组的 操作站点-物料对象 结合
     */
    @Override
    public TopCentralOperationStationMaterial selectByOperationStationId(Integer operationStationId) {
        TopCentralOperationStationMaterial params = new TopCentralOperationStationMaterial();
        params.setOperationStationId(operationStationId);
        return selectOne(params);
    }

    /**
     * 查询操作站点-物料 根据物料编号
     * @param materialId 物料编号
     * @return 返回分组的 操作站点-物料对象 结合
     */
    @Override
    public TopCentralOperationStationMaterial selectByMaterialId(String materialId) {
        TopCentralOperationStationMaterial params = new TopCentralOperationStationMaterial();
        params.setMaterialId(materialId);
        return selectOne(params);
    }

    /**
     * 获取所有的操作站点-物料数据
     * 
     * @return Map<Integer,TopCentralOperationStationMaterial> 集合
     */
    @Cacheable
    @Override
    public Map<Integer,TopCentralOperationStationMaterial> selectAllMap(){

        return getBaseMapper().selectList( lambdaQuery())
                .stream().collect(Collectors.toMap(TopCentralOperationStationMaterial::getId,v->v));
    }

    /**
     * 新增操作站点-物料
     *
     * @param topCentralOperationStationMaterial 操作站点-物料
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(TopCentralOperationStationMaterial topCentralOperationStationMaterial)
    {

        PublisherUtils.publishEvent(OperationStationMaterialEvent.class,topCentralOperationStationMaterial, SignEnum.INSERT);

        return super.insert(topCentralOperationStationMaterial);
    }

    /**
     * 新增（批量）操作站点-物料
     *
     * @param topCentralOperationStationMaterialList 实体对象集合
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean insertBatch(Collection<TopCentralOperationStationMaterial> topCentralOperationStationMaterialList, int batchSize) {
        return super.insertBatch(topCentralOperationStationMaterialList, batchSize);
    }

    /**
     * 删除 操作站点-物料 根据编号
     * @param id
     * @return true 删除成功 false 删除失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean deleteById(Serializable id) {
        return super.deleteById(id);
    }

    /**
     * 批量删除 操作站点-物料 根据 编号集合
     * @param ids
     * @return true 删除成功 false 删除失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean deleteByIds(Collection<? extends Serializable> ids) {
        return super.deleteByIds(ids);
    }

    /**
     * 批量修改 操作站点-物料 根据唯一编号
     * @param topCentralOperationStationMaterialList
     * @param batchSize  更新批次数量
     * @return true 修改成功 false 修改失败
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateBatchById(Collection<TopCentralOperationStationMaterial> topCentralOperationStationMaterialList, int batchSize) {
        return super.updateBatchById(topCentralOperationStationMaterialList, batchSize);
    }

    /**
     * 修改 操作站点-物料
     * @param  topCentralOperationStationMaterial
     * @param updateWrapper
     * @return
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean update(TopCentralOperationStationMaterial topCentralOperationStationMaterial, LambdaUpdateWrapper<TopCentralOperationStationMaterial> updateWrapper) {
        return super.update(topCentralOperationStationMaterial,updateWrapper);
    }

    /**
     * 修改操作站点-物料
     *
     * @param topCentralOperationStationMaterial 操作站点-物料
     * @return 结果
     */
    @CacheEvict(keyGenerator=CacheConstants.KeyGeneratorName.EHCACHE_KEY_GENERATOR_EVICT)
    @Override
    public boolean updateById(TopCentralOperationStationMaterial topCentralOperationStationMaterial)
    {
        return super.updateById(topCentralOperationStationMaterial);
    }


}
